<?php
// $Id: heartbeatparser.inc,v 1.1.2.3.2.17 2009/11/15 14:41:53 stalski Exp $

class heartbeatParser
{
  private $_candidates = array();
  private $_messages = array();
  private $_sets = array();
  private $_timespan_gap = 0;
  private $_allowed_tags = array('p','span','a','em','strong','u','i','blockquote');
  public $raw_messages = array();

  private function __construct() {
  	$this->_allowed_tags = heartbeat_allowed_tags();
  }

  /**
   * Get the tags to allow in messages
   */
  public function get_tags() {
    return $this->_allowed_tags;
  }

  /**
   * Creates an instance of heartbeat as singleton
   */
  static public function instantiate($type='cached') {
    static $instances;
    if(!$instances) {
      $instances = array();
    }
    if(!isset($instances[$type])) {
      $instances[$type] = new HeartbeatParser();
    }
    return $instances[$type];
  }

  /**
   * Function to remove duplicate messages
   * Duplicate messages whish are exactly the same as already build messages,
   *   delete them and increment the count of the message instance
   * Duplicate reversive messages that occur at the same server request
   *   time are deleted as well
   */
  private function remove_message_duplicates($message_set) {

    global $user;

    // hash holding all unique values
    // if this would be static ...you could do it for all timespan-sets
    $holder = array();
    $duplicates = array();
    foreach ($message_set as $key => $message) {

      // if an exact identical message occurs > twice contextual wise,
      // then skip it with an incrementation of the frequency
      // of the message
      $id = $message->message_id .'-'. $message->uid .'-'. $message->uid_target .'-'. $message->nid .'-'. $message->nid_target;
	  if (in_array($id, $holder)) {
        $message_set[array_search($id, $holder)]->count++;
        unset($message_set[$key]);
      }
      else {
        $holder[$key] = $id;//$message->message;
      }

      // Remove duplicates that were created by duplicate user logging
      // This happens when two users were active for the same event
      if (isset($message->variables['duplicate']) && $message->variables['duplicate']) {
      	$duplicates[$message->uid_target .'_'. $message->uid] = true;
      }
      // When its duplicate comes arround, delete it
      if (isset($duplicates[$message->uid .'_'. $message->uid_target])) {
      	unset($message_set[$key]);
      }
    }
    return $message_set;
  }

  /**
   * Prepare message candidates
   * This is an important method that handles several things
   * - Build an id for each time-gap limiting groupable messages
   * - Hold the candidates for each id (and count of messages)
   * - Logic to handle the group_by node or user setting
   */
  private function prepare_candidates($message_set, $set_count=0) {

    static $singles = 0;

    // Remove duplicate messages in the same set,
    // incrementing the counter on the grouped message
    $message_set = $this->remove_message_duplicates($message_set);

    foreach ($message_set as $key => $message) {

      if (!$message->nid_access || !$message->nid_target_access || !$message->uid_access) {
      	continue;
      }

      $type = $message->concat_args['type'];

      // Variable to hold the unique grouping gap id.
      // Extending the gap id will result in summaries
      // and groups of identical and related messages.
      $gap_id = 'BEAT_' . $set_count . '_' . $message->message_id;

      // Summaries have to be evaluated for merging
      if ($type == 'summary' && $this->extend_gap_id($gap_id, $message, $type, $set_count)) {

        // Add a candidates if this one does not exist yet
        if (!isset($this->_candidates[$gap_id])){
          $this->_candidates[$gap_id] = array(
            'count' => 0,
            'group_target' => $message->concat_args['group_target'],
            'variables' => array()
          );
          // Add the message
          $this->_messages[$gap_id] = $message;
        }
        // Make sure the the timestamp gets updated
        else {
          $this->_messages[$gap_id]->timestamp = $message->timestamp;
        }

        // Add the counters and variables needed for merging in groups
        $this->_messages[$gap_id]->target_count++; // Message occurrency, first time is 1

        $this->_candidates[$gap_id]['variables'][] = $message->variables;
        $this->_candidates[$gap_id]['count']++;  // variable count, NOT the same as target_count
      }
      elseif ($type == 'count') {

        //$gap_id = $gap_id . '_'.$message->uid;
        $gap_id = $gap_id . '_count';
        $this->_messages[$gap_id] = $message;
        $this->_messages[$gap_id]->target_count++;

      }
      else {

        // Autoincrement the singles to make the gap_id unique
        $gap_id .= '_single_'. $singles;
        $this->_messages[$gap_id] = $message;
        $singles++;
      }
    }

  }

  /**
   * Function to clean up dirty messages and xss attacks
   */
  private function clean_up() {
    $this->remove_variables_duplicates();
  }

  /**
   * Function to remove duplicate messages valuating the
   * variables and count properties
   */
  private function remove_variables_duplicates() {

    $uniques = array();
    foreach($this->_candidates as $single_id => $info) {
      $uniques[$single_id] = array();
      // Loop through variables to check if there are identical
      foreach($info['variables'] as $rid => $value) {
        if(!in_array($value, $uniques[$single_id])) {
          $uniques[$single_id][] = $value;
        }
        else {
          unset($this->_candidates[$single_id]['variables'][$rid]);
        }
      }
    }

    // Re-evaluate the counts
    foreach($this->_candidates as $single_id => $info) {
      $this->_candidates[$single_id]['count'] = count($this->_candidates[$single_id]['variables']);
    }

    return $uniques;
  }

  /**
   * Private helper function to build the gap_id
   */
  private function extend_gap_id(&$gap_id, $message, $type, $set_count) {

    // Extend the gap id with the node_type if available
    // but this is too dangerous because i don't know if people intend to
    // separate the node types from merging ...
    if (!empty($message->variables['@node_type'])) {
      $gap_id .= '_' . $message->variables['@node_type'];
    }

    // group by node will result in a summary of users
    // performing the activity
    if ($message->concat_args['group_by'] == 'node') {

      $gap_id .= '_' . $message->concat_args['group_target'] . '_node_' . $message->nid;
    }
    // group by user will result in a summary of content
    // nodes which the user has done the activity on
    else if($message->concat_args['group_by'] == 'user') {

      $gap_id .= '_' . $message->concat_args['group_target'] . '_user_' . $message->uid;
      if ($message->nid_target > 0) {
        $gap_id .= '_'. $message->nid_target;
      }
    }

    // group by user-user will result in unique user-user
    // messages, expressing a relation between the two users
    else if($message->concat_args['group_by'] == 'user-user') {

      $gap_id .= '_user_relation';
      //$gap_id .= '_'. ($target_uid == 0 ? $message->uid : $target_uid);
      $group_target = $message->concat_args['group_by_target'];
      $string = $message->variables['@' . $group_target];
      $group_gap_string = strip_tags(strtolower($string));
      $gap_id .= '_' . $group_gap_string;
    }
    else {

      // Handle the fall-backs as unique messages
      $gap_id .= '_' . $message->uid .'_' . (int)$message->uid_target;
    }

    return TRUE;
  }

  /**
   * Function to rebuild the messages in groups
   * all sets are handled already
   */
  private function rebuild_in_groups() {

    global $base_url;

    $max_items_to_group = variable_get('heartbeat_activity_grouping_how_many', 6);

    // Check the candidates and make the rebuild array
    foreach ($this->_candidates as $single_id => $info) {

      // if a candidate exists for this message and it has more than one count
      // Take care!! not message->count because this could be set with the sql as well
      // The purpose would be to fill %times% or its alias %count%
      if ($this->_candidates[$single_id]['count'] > 1) {

      	// rebuild the message using the candidate variables
      	$this->_messages[$single_id]->create_grouped_message($info, $max_items_to_group);

      }
    }
  }

  /**
   * Remove broken messages
   * @Todo build this function
   */
  public function remove_broken_messages($messages=array()) {
    if($messages == array()) {
      $messages = $this->_messages;
    }
    return $messages;
  }

  /**
   * Set the first time , start time of the gap
   */
  public function set_timespan_gap($gap) {
    $this->_timespan_gap = $gap;
  }

  /**
   * build sets of messages
   */
  public function build_sets($messages_raw) {

  	// The start is the end of the current day
  	$renewal_time = $_SERVER['REQUEST_TIME'] - (3600 * date("H") + 60 * date("i") + date("s")) + (24 *3600);

    $raw_messages = array();
    foreach($messages_raw as $key => $message) {
      // if the time with the gap exceeds the starttime
      if($renewal_time >= $message->timestamp) {
        // reset the start time of the set
        $renewal_time = $message->timestamp - $this->_timespan_gap;
      }
      $this->_sets[$renewal_time][$key] = $message;
    }
  }

  /**
   * Merges sets of messages to fully formatted messages
   * regenerated at runtime to group settings
   */
  public function merge_sets() {

    $set_count = 0;
	  foreach($this->_sets as $message_set) {
      $this->prepare_candidates($message_set, $set_count);
      $set_count++;
    }

    $this->clean_up();

    $this->rebuild_in_groups();

    return $set_count;
  }

  /**
   * Gets the messages as they exist for the time asked
   */
  public function get_messages($limit = 0) {
    if($limit > 0) {
      return array_slice($this->_messages, 0, $limit);
    }
    return $this->_messages;
  }
}

// eof